home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: need for function prototypes
- Date: Tue, 27 Feb 96 20:10:58 GMT
- Organization: none
- Message-ID: <825451858snz@genesis.demon.co.uk>
- References: <4gutho$o1a@mn5.swip.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4gutho$o1a@mn5.swip.net>
- chris.rossall@mailbox.swipnet.se "Chris Rossall" writes:
-
- >Hello I am having trouble understanding why I should use function
- >prototypes. I mean,if the function is defined before it is used,the
- >compiler should have enough information about the parameters to
- >produce correct code anyway.
-
- The first thing to do here is to make the terminology perfectly clear.
- Consider:
-
-
- 1. int foo();
-
- 2. int foo(float);
-
- 3. int foo(value)
- float value;
- {
- }
-
- 4. int foo(float value)
- {
- }
-
- All of these are 'declarations'.
-
- 3 and 4 are 'definitions' because they allocate storage (i.e. in this
- case include a function body).
-
- Of these four only 2 and 4 contain prototypes i.e. the parameter list
- contains type information. In the case of 3 the parameter list is simply
- value and the type is specified elsewhere - it is not a prototype.
-
- Only prototypes provide type information for the compiler to use with
- function calls. If I write:
-
- foo(1);
-
- the action will depend on which of the four is in scope. With 1 and 3 the
- compiler will simply pass the integer value 1. Since this is incompatible
- with the parameter type the result is undefined behaviour. With 2 or 4
- in scope the compiler must convert the value to a float (where such a
- conversion is legal as it is in this case) and pass that. If the conversion
- is not legal the compiler must warn about it which provides important
- diagnostics.
-
- The key case here is 3 - having such a definition in scope does not guarantee
- you any error checking or implicit conversion, although a particular compiler
- can generate any extra diagnostics it likes and one form of undefined
- behaviour is 'doing what seems reasonable'.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-